//
//  JTableHelper.m
//
//  jhf
//
//  Created by 计海峰 on 2020/8/16.
//

#import "JTableHelper.h"
#import <CooperSDK/CooperSDK.h>
#import "TableVC.h"

@implementation JTableHelper

// MARK: - push
/// 跳转到vc
/// @param vc vc
void routerTo(UIViewController *vc) {
    UIViewController *currentVC = currentTopVC();
    if (vc && currentVC && currentVC.navigationController) {
        [currentVC.navigationController pushViewController:vc animated:true];
    }
}

/// 跳转到vc
/// @param vmStr vmStr
/// @param title navbar title
TableVC* routerToByRowsVM(NSString *vmStr, NSString *title) {
    TableVC *vc = [TableVC vcWithClassStr:vmStr];
    vc.navigationItem.title = title;
    routerTo(vc);
    return vc;
}

/// 跳转到vc
/// @param vmStr vmStr
/// @param title navbar title
/// @param keys 参数key
/// @param values 参数value
TableVC* routerToByPageVM(NSString *vmStr, NSString *title, NSArray *keys, NSArray *values) {
    TableVC *vc = [TableVC vcWithClassStr:vmStr];
    vc.navigationItem.title = title;
    vc.enablePull = true;
    if (keys.count > 0 && keys.count == values.count) {
        [vc configKeys:keys values:values];
    }
    routerTo(vc);
    return vc;
}

/// push 返回上一级
void navBack(void) {
    UIViewController *currentVC = currentTopVC();
    if (currentVC && currentVC.navigationController) {
        [currentVC.navigationController popViewControllerAnimated:true];
    }
}

/// push 返回上级
/// @param num 数量
void navBackTo(int num) {
    UIViewController *currentVC = currentTopVC();
    if (currentVC && currentVC.navigationController) {
        [JUtil popNumController:num orToNumController:-1 andVC:currentVC];
    }
}

// MARK: - present
/// 跳转到vc
/// @param vc vc
void presentTo(UIViewController *vc, BOOL isNav) {
    UIViewController *currentVC = currentTopVC();
    if (vc && currentVC) {
        if (isNav) {
            UIViewController *navVC = [[AGGNavigationController alloc] initWithRootViewController:vc];
            vc.fd_prefersNavigationBarHidden = true;
            navVC.modalPresentationStyle = UIModalPresentationPageSheet;
            [currentVC presentViewController:navVC animated:true completion:nil];
            return;
        }
        [currentVC presentViewController:vc animated:true completion:nil];
    }
}

/// 跳转到vc
/// @param vmStr vmStr
/// @param title navbar title
TableVC* presentToByRowsVM(NSString *vmStr, NSString *title, BOOL isNav) {
    TableVC *vc = [TableVC vcWithClassStr:vmStr];
    vc.navigationItem.title = title;
    presentTo(vc, isNav);
    return vc;
}

/// 跳转到vc
/// @param vmStr vmStr
/// @param title navbar title
/// @param keys 参数key
/// @param values 参数value
TableVC* presentToByPageVM(NSString *vmStr, NSString *title, NSArray *keys, NSArray *values, BOOL isNav) {
    TableVC *vc = [TableVC vcWithClassStr:vmStr];
    vc.navigationItem.title = title;
    vc.enablePull = true;
    if (keys.count > 0 && keys.count == values.count) {
        [vc configKeys:keys values:values];
    }
    presentTo(vc, isNav);
    return vc;
}

/// dismiss 返回上一级
void dismissBack(void) {
    UIViewController *currentVC = currentTopVC();
    if (currentVC) {
        [currentVC dismissViewControllerAnimated:true completion:nil];
    }
}

// MARK: - remove
/// 删除navs 里面的几个table 配置的 controller
/// @param vms 对应的key
/// @param nav nav 可为空 默认取当前的
void removeNavTabVCs(NSArray <NSString *>*vms, UINavigationController* _Nullable nav) {
    UINavigationController *targetNav = nav;
    if (nav == nil) {
        targetNav = currentTopVC().navigationController;
    }
    NSMutableArray *viewControllers = [targetNav.viewControllers mutableCopy];
    for (UIViewController *viewController in [viewControllers reverseObjectEnumerator]) {
        if ([viewController isKindOfClass:TableVC.class]) {
            TableVC *vc = (TableVC *)viewController;
            NSString *tempName = NSStringFromClass(vc.vm.class);
            if ([vms containsObject:tempName]) {
                [viewControllers removeObject:viewController];
            }
        }
    }
    [targetNav setViewControllers:viewControllers animated:YES];
}

/// 删除navs 里面的几个table 配置的 controller
/// @param keys 对应绑定的 bindKey
/// @param nav nav 可为空 默认取当前的
void removeNavVcByKeys(NSArray <NSString *> *keys, UINavigationController* _Nullable nav) {
    UINavigationController *targetNav = nav;
    if (nav == nil) {
        targetNav = currentTopVC().navigationController;
    }
    NSMutableArray * viewControllers = [targetNav.viewControllers mutableCopy];
    for (UIViewController *viewController in [viewControllers reverseObjectEnumerator]) {
        if ([viewController isKindOfClass:TableVC.class]) {
            TableVC *tb = (TableVC *)viewController;
            if ([keys containsObject:tb.vm.bindKey]) {
                [viewControllers removeObject:viewController];
            }
        }
    }
    [targetNav setViewControllers:viewControllers animated:YES];
}


/// 顶部vc
UIViewController *currentTopVC(void) {
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    UIViewController *vc = [window currentTopViewController];
    if(jBaseSDK.debugModel) {
        NSLog(@"顶部vc: %@", vc);
    }
    return vc;
}

@end
